package q2;

import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketTCPQ2
{
	public ServerSocketTCPQ2(int iPort )
	{
		System.out.print("\n\nServerSocketTCPQ2 SERVER INSTANCE\n");

		try
		{
			//	Create the TCP server
			ServerSocket serverSocketIn = new ServerSocket( iPort );
			
			do
			{
				System.out.print("ServerSocketTCPQ2 : Waiting for incoming connections.\n");

				//	ServerSocket.accept method will block until an incoming client arrive
				Socket sktIn = serverSocketIn.accept();
				System.out.println("A new client has connected.    DEBUG InputSocket : " + sktIn );
				
				//	Start a new Http server when we have a new connection
				ServerHttpQ2 serverHttp = new ServerHttpQ2( sktIn );

			//	TODO find a way to exit properly...
			} while( true );
	
		}
		catch(Exception e)
		{
			System.out.print("ServerSocketTCPQ2 : Error while waiting for incoming connections : \n" + e.toString() + "\n");
		}
	}
}
